package cn.swing.his.finance.webapi.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.JsonResult;
import cn.swing.his.finance.service.IFinanceDetailService;
import cn.swing.his.pojo.finance.dto.AddFinanceDetailDTO;
import cn.swing.his.pojo.finance.dto.TimeDTO;
import cn.swing.his.pojo.finance.entity.FinanceDetail;
import cn.swing.his.pojo.finance.vo.FinanceDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;


@RestController
@RequestMapping("finance/detail")
@Api(tags = "财务收支明细模块")
@Slf4j
public class FinanceDetailController {
    @Autowired
    private IFinanceDetailService financeDetailService;

    @ApiOperation(value = "查询后台财务明细")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(value="页数",name="pageNum",example = "1"),
            @ApiImplicitParam(value="条数",name="pageSize",example = "10")})
    public JsonResult<JsonPage<FinanceDetailVO>> pageFinanceDetails(Integer pageNum,Integer pageSize){
        log.debug("pageNum:{}",pageNum);
        log.debug("pageSize:{}",pageSize);
        JsonPage<FinanceDetailVO> jsonPage=financeDetailService.financeDetailList(pageNum, pageSize);
        return JsonResult.ok("成功",jsonPage);
    }
    /**
     * 新增财务明细
     * @param addFinanceDetailDTO
     * @return
     */
    @ApiOperation(value = "新增财务明细")
    @PostMapping("/addDetail")
    public JsonResult<Void> addFinanceDetail(AddFinanceDetailDTO addFinanceDetailDTO){
        financeDetailService.insertFinanceDetail(addFinanceDetailDTO);
        return JsonResult.ok();
    }

    /**
     * 根据时间导出
     * @param response
     * @param startDate
     * @param endDate
     * @throws Exception
     */
    @ApiOperation(value = "根据时间导出明细表")
    @GetMapping("/export")
    public void exportDetail(HttpServletResponse response,String startDate,String endDate) throws Exception {
        TimeDTO time = new TimeDTO();
        time.setStartDate(startDate);
        time.setEndDate(endDate);
        List<FinanceDetail> list = financeDetailService.financeDetailAllList(time);
        ExcelWriter writer = ExcelUtil.getWriter(true);
//        ExcelWriter writer = ExcelUtil.getWriter(true);
        log.info("list:{}",list);
        log.info("成功");
        //自定义标题别名
        writer.addHeaderAlias("id","id");
        writer.addHeaderAlias("name", "交易人");
        writer.addHeaderAlias("type", "收支类型");
        writer.addHeaderAlias("price", "金额");
        writer.addHeaderAlias("dealTime", "交易时间");
        writer.addHeaderAlias("description", "交易描述");
        writer.addHeaderAlias("gmtCreated", "创建时间");
        writer.addHeaderAlias("gmtModified", "修改时间");
        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("收支信息(时间)", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
        log.debug("成功！");
    }
}
